//
//  YCCarStyleCell.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/29.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  车型cell

#import "YCCarStyleCell.h"

@interface YCCarStyleCell ()

/** 背景视图  */
@property (nonatomic, strong) UIView *bgView;
/** 选中图片  */
@property (nonatomic, strong) UIImageView *selectedImageView;

/** 车图片 */
@property (nonatomic, weak) UIImageView *iconImageView;
/** 车型大小号  */
@property (nonatomic, weak) UILabel *carSizeLabel;
/** 车型  */
@property (nonatomic, weak) UILabel *carStyleLabel;
/** 费用标签  */
@property (nonatomic, weak) UILabel *costLabel;

@end

@implementation YCCarStyleCell

/**
 *  快速创建cell
 *
 *  @param tableView cell所在的表
 *  @param indexPath cell位置
 *
 *  @return 返回cell对象
 */
+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"carStyleCell";
    YCCarStyleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[YCCarStyleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    }
    return cell;
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.selectedImageView];
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(13);
            make.right.equalTo(self.contentView).offset(-13);
            make.top.equalTo(self.contentView).offset(6);
            make.bottom.equalTo(self.contentView).offset(-6);
        }];
        
        [self.selectedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.width.equalTo(@(widthFromIphone6Width(50)));
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView).offset(-heightFromIphone6Height(30));
        }];
    }
    return self;
}


- (UIImageView *)selectedImageView
{
    if (!_selectedImageView) {
        _selectedImageView = [[UIImageView alloc] init];
        _selectedImageView.image = [UIImage imageNamed:@"ms_select"];
        _selectedImageView.contentMode = UIViewContentModeCenter;
        _selectedImageView.hidden = YES;
    }
    return _selectedImageView;
}


- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 5;
        _bgView.layer.masksToBounds = YES;
        
        // 车图片视图
        UIImageView *iconImageView = [[UIImageView alloc] init];
        iconImageView.contentMode = UIViewContentModeCenter;
        self.iconImageView = iconImageView;
        [iconImageView yc_setWebImageWithURLString:nil holderSize:CGSizeMake(120, 100)];
        
        // 车型大小号
        UILabel *carSizeLabel = [[UILabel alloc] init];
        carSizeLabel.font = [UIFont systemFontOfSize:15];
        carSizeLabel.textAlignment = NSTextAlignmentLeft;
        carSizeLabel.text = @"小型车(C1/C2)";
        carSizeLabel.textColor = [UIColor blackColor];
        self.carSizeLabel = carSizeLabel;
        
        // 车型
        UILabel *carStyleLabel = [[UILabel alloc] init];
        carStyleLabel.font = [UIFont systemFontOfSize:13];
        carStyleLabel.textAlignment = NSTextAlignmentLeft;
        carStyleLabel.text = @"车型 宝来";
        carStyleLabel.textColor = [UIColor yc_colorWithUInt:0x808080];
        self.carStyleLabel = carStyleLabel;
        
        // 费用
        UILabel *costLabel = [[UILabel alloc] init];
        costLabel.font = [UIFont systemFontOfSize:15];
        costLabel.textAlignment = NSTextAlignmentLeft;
        costLabel.text = @"¥5800";
        costLabel.textColor = [UIColor yc_colorWithUInt:0xff4055];
        self.costLabel = costLabel;
        
        [_bgView addSubview:iconImageView];
        [_bgView addSubview:carStyleLabel];
        [_bgView addSubview:carSizeLabel];
        [_bgView addSubview:costLabel];
        
        // 布局
        
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bgView).offset(widthFromIphone6Width(10));
            make.centerY.equalTo(_bgView);
            make.width.equalTo(@(widthFromIphone6Width(120)));//120
            make.height.equalTo(iconImageView.mas_width).multipliedBy(0.83333);//100
        }];
        // 车型 中间
        [carStyleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconImageView.mas_right).offset(10);
            make.right.equalTo(_bgView).offset(10);
            make.centerY.equalTo(_bgView);
        }];
        // 车大小 上
        [carSizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(carStyleLabel);
            make.bottom.equalTo(carStyleLabel.mas_top).offset(-6);
        }];
        // 费用 下
        [costLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(carStyleLabel);
            make.top.equalTo(carStyleLabel.mas_bottom).offset(14);
        }];
        
    }
    return _bgView;
}


- (void)setModel:(YCSelectCarModel *)model
{
    _model = model;
    [self.iconImageView yc_setWebImageWithURLString:model.img holderSize:CGSizeMake(120, 100)];
    self.carStyleLabel.text = model.type;
    self.carSizeLabel.text = model.title;
    self.costLabel.text = model.price;
    if (model.isSelected) {
        self.selectedImageView.hidden = NO;
    } else {
        self.selectedImageView.hidden = YES;
    }
    self.bgView.backgroundColor = model.isSelected ? [UIColor yc_colorWithUInt:0xd0d0d0]:[UIColor whiteColor];
}

@end
